package aip.core.dao.todo;

import static org.springframework.util.Assert.hasText;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.dao.DataAccessException;

import com.googlecode.horncomb.bean.filter.ConditionSet;
import com.googlecode.horncomb.bean.filter.SimpleMapConditionSet;
import com.googlecode.horncomb.bean.order.DefaultOrder;
import com.googlecode.horncomb.bean.order.DefaultOrderToken;
import com.googlecode.horncomb.bean.order.Order;
import com.googlecode.horncomb.orm.ibatis.SqlMapEntityDaoImpl;

import aip.core.model.todo.Todo;
import aip.ui.user.util.UserHelper;

/**
 * Todo data access object
 * <p>
 * 负责人/principal:<br/>
 * 修改记录/revision:<br/>
 * 日期:<br/>
 * 修改人:<br/>
 * 修改说明:<br/>
 * @author
 * @version $Id$
 */
public class TodoDao extends SqlMapEntityDaoImpl<Todo>
{
    private static final long serialVersionUID = 6314306216775940804L;

    public List<? extends Todo> queryByDomain(int domain)
            throws DataAccessException
    {
        ConditionSet cons = new SimpleMapConditionSet().addConstraint("domain",
                domain);
        Order order = new DefaultOrder(new DefaultOrderToken("CREATE_", false),
                new DefaultOrderToken("DONE_", false));
        return super.query(cons, order);
    }

    public void update(Integer domain, Todo nextTodo)
            throws DataAccessException
    {
        Date now = new Date();
        ConditionSet cons = new SimpleMapConditionSet().addConstraint("domain",
                domain).addConstraint("actor",
                UserHelper.getUser().getUsername()).addConstraint("done", now)
                .addConstraint("state", Todo.STATE_CONS_DONE);
        this.doUpdate(cons, null, "updateDone");
        if (nextTodo != null)
        {
            hasText(nextTodo.getTitle());
            hasText(nextTodo.getState());

            nextTodo.setCreate(now);
            nextTodo.setDomain(domain);
            this.insert(nextTodo);
        }
    }

    /**
     * 完成当前待办，如存在下一步骤，则插入下一待办
     * @param domains
     * @param nextActor
     * @throws DataAccessException
     */
    public void update(Set<Integer> domains, Todo nextTodo)
            throws DataAccessException
    {
        Date now = new Date();
        ConditionSet cons = new SimpleMapConditionSet().addConstraint(
                "domains", domains.toArray(new Integer[] {})).addConstraint(
                "actor", UserHelper.getUser().getUsername()).addConstraint(
                "done", now).addConstraint("state", Todo.STATE_CONS_DONE);
        this.doUpdate(cons, null, "updateDone");
        if (nextTodo != null)
        {
            hasText(nextTodo.getActor());
            hasText(nextTodo.getTitle());
            for (Integer domain : domains)
            {
                nextTodo.setCreate(now);
                nextTodo.setDomain(domain);
                this.insert(nextTodo);
            }
        }
    }
}
